package com.ecoolex.discount.sales.admin.graphql;

import com.ecoolex.discount.sales.admin.core.AdminConstant;
import com.ecoolex.discount.sales.entity.DiscountProductWithBLOBs;
import com.ecoolex.discount.sales.entity.MarketingProduct;
import com.ecoolex.discount.sales.pojo.AddMarketingProductDTO;
import com.ecoolex.discount.sales.pojo.QueryMarketingProductDTO;
import com.ecoolex.discount.sales.ws.DiscountProductService;
import com.ecoolex.discount.sales.ws.MarketingProductService;
import com.ecoolex.framework.common.enums.ResultCode;
import com.ecoolex.framework.common.pojo.Page;
import com.ecoolex.framework.common.pojo.Pagination;
import com.ecoolex.framework.common.pojo.Sorting;
import com.ecoolex.framework.common.util.Assert;
import com.ecoolex.framework.common.util.Check;
import com.ecoolex.framework.modularize.api.auth.admin.AdminMemberAuthority;
import com.ecoolex.framework.modularize.api.auth.annotation.AdminAuthoritativeRegister;
import com.ecoolex.framework.web.api.graphql2.annotation.*;
import com.ecoolex.framework.web.api.graphql2.annotation.GraphQL2Module.Type;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.lang.NonNull;

@GraphQL2Module(type = Type.Admin, description = "产品营销")
@AdminAuthoritativeRegister(parentKey = AdminConstant.AUTH_PROJECT_KEY,
        key = MarketingProductGraphql.AUTH,
        name = "产品营销管理",
        remark = "产品营销管理")
public class MarketingProductGraphql {

    public static final String AUTH = AdminConstant.AUTH_PROJECT_KEY + "-MARKETING-PRODUCT";

    @Reference
    private MarketingProductService marketingProductService;

    @Reference
    private DiscountProductService discountProductService;

    @TypeField(type = MarketingProduct.class, field = "discountProduct", description = "折扣产品")
    public DiscountProductWithBLOBs discountProduct(@Source MarketingProduct source) {
        return discountProductService.getProductById(source.getProductId());
    }

    @TypeField(type = MarketingProduct.class, field = "canEdit", description = "能编辑")
    public boolean canEdit(@Source MarketingProduct source, AdminMemberAuthority user) {
        return user.auth(AUTH_EDIT);
    }

    @TypeField(type = MarketingProduct.class, field = "canDel", description = "能删除")
    public boolean canDel(@Source MarketingProduct source, AdminMemberAuthority user) {
        return user.auth(AUTH_DEL);
    }

    public static final String AUTH_GET = AUTH + "-GET";

    @QueryField(field = "marketingProduct", description = "产品营销详情")
    @AdminAuthoritativeRegister(parentKey = AUTH, key = AUTH_GET, name = "产品营销详情")
    public MarketingProduct get(@NonNull Integer id, AdminMemberAuthority user) {
        user.assertAuth(AUTH_GET);
        return marketingProductService.getMarketingProduct(id);
    }

    public static final String AUTH_QUERY = AUTH + "-QUERY";

    @QueryField(field = "marketingProductList", description = "产品营销列表")
    @AdminAuthoritativeRegister(parentKey = AUTH, key = AUTH_QUERY, name = "产品营销列表")
    public Pagination<MarketingProduct> query(@NonNull QueryMarketingProductDTO query, @NonNull Sorting[] sorts, @NonNull Page page, AdminMemberAuthority user) {
        user.assertAuth(AUTH_QUERY);
        return marketingProductService.queryMarketingProduct(query, sorts, page);
    }

    public static final String AUTH_ADD = AUTH + "-ADD";

    @MutationField(field = "addMarketingProduct", description = "添加产品营销")
    @AdminAuthoritativeRegister(parentKey = AUTH, key = AUTH_ADD, name = "添加产品营销")
    public MarketingProduct add(@NonNull AddMarketingProductDTO record, AdminMemberAuthority user) {
        user.assertAuth(AUTH_ADD);
        return marketingProductService.addMarketingProduct(record, user.userName());
    }

    public static final String AUTH_EDIT = AUTH + "-EDIT";

    @MutationField(field = "editMarketingProduct", description = "编辑产品营销权重")
    @AdminAuthoritativeRegister(parentKey = AUTH, key = AUTH_EDIT, name = "编辑产品营销权重")
    public MarketingProduct edit(@NonNull Integer id, @NonNull Integer weight, AdminMemberAuthority user) {
        user.assertAuth(AUTH_EDIT);
        MarketingProduct marketingProduct = marketingProductService.getMarketingProduct(id);
        Assert.isTrue(Check.notNull(marketingProduct), ResultCode.VERIFY.build(1), "产品营销不存在");
        marketingProduct.setWeight(weight);
        return marketingProductService.editMarketingProduct(marketingProduct, user.userName());
    }

    public static final String AUTH_DEL = AUTH + "-DEL";

    @MutationField(field = "delMarketingProduct", description = "删除产品营销")
    @AdminAuthoritativeRegister(parentKey = AUTH, key = AUTH_DEL, name = "删除产品营销")
    public boolean delete(@NonNull Integer id, AdminMemberAuthority user) {
        user.assertAuth(AUTH_DEL);
        MarketingProduct marketingProduct = marketingProductService.getMarketingProduct(id);
        Assert.isTrue(Check.notNull(marketingProduct), ResultCode.VERIFY.build(1), "产品营销不存在");
        marketingProductService.deleteMarketingProduct(marketingProduct, user.userName());
        return true;
    }
}
